package auth

import (
	"github.com/dgrijalva/jwt-go"
	"time"
)

var (
	Key = []byte("123456")
	Expiry int64 = 3600*24
)

type Claims struct {
	jwt.StandardClaims
	UserID            int64
	Account           string
	AccessControlList string // 访问控制表
	Extends           map[string]interface{} // 各模块自行扩展，添加信息后重新生成token，刷新token时保留
}

func Generate(claims Claims) string {
	claims.StandardClaims = jwt.StandardClaims{
		ExpiresAt: time.Now().Unix() + Expiry,
	}
	t := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
	token, err := t.SignedString(Key)
	if err != nil {
		panic(err)
	}
	return token
}

func KeyProvider(token *jwt.Token) (i interface{}, err error) {
	return Key, nil
}
